package com.qezhhnjy.bilibili.controller;

import com.qezhhnjy.bilibili.entity.CommonResult;
import com.qezhhnjy.bilibili.entity.Payment;
import com.qezhhnjy.bilibili.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author zhaoyangfu - 2021/4/3 15:24
 */
@RestController
@RequestMapping("/payment")
@Slf4j
public class PaymentController {

    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String port;

    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("/get")
    public CommonResult<Payment> get(Long id) {
        return CommonResult.success(paymentService.getById(id));
    }

    @PostMapping("/create")
    public CommonResult<?> create(@RequestBody Payment payment) {
        paymentService.save(payment);
        return CommonResult.success(port);
    }

    @PutMapping("/update")
    public CommonResult<?> update(@RequestBody Payment payment) {
        paymentService.updateById(payment);
        return CommonResult.success();
    }

    @GetMapping("/discovery")
    public Object discovery() {
        // 返回eureka中的所有服务信息
        List<String> services = discoveryClient.getServices();
        services.forEach(s -> log.info("service=>{}", s));
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");

        instances.forEach(i -> log.info("instance=>{}", i.getMetadata()));
        return discoveryClient;
    }

}
